﻿using System;
using System.Windows;
using System.Windows.Input;

public class AppletUI : UI
{
    internal FrameworkElement applet;
    internal KbInputHandler kbJoy1;
    internal KbInputHandler kbJoy2;
    internal NES nes;
    internal int sleepTime;
    private static int state;
    internal long t1;
    internal long t2;
    internal ScreenView vScreen;

    public AppletUI(FrameworkElement applet)
    {
        this.applet = applet;
        this.nes = new NES(this);
    }

    public override void imageReady(bool skipFrame)
    {
        if (!skipFrame)
        {
            this.nes.getPapu().writeBuffer();
        }
        this.nes.cpu.Emulating = false;
    }

    public override void init(bool showGui)
    {
        this.vScreen = new ScreenView(this.nes, 0x100, 240);
        this.vScreen.init();
        this.vScreen.NotifyImageReady = true;
        this.kbJoy1 = new KbInputHandler(this.nes, 0);
        this.kbJoy2 = new KbInputHandler(this.nes, 1);
        //this.kbJoy1.mapKey(InputHandler.KEY_A, (int) Key.Z);
        //this.kbJoy1.mapKey(InputHandler.KEY_B, (int) Key.X);
        //this.kbJoy1.mapKey(InputHandler.KEY_START, (int)Key.Enter);
        //this.kbJoy1.mapKey(InputHandler.KEY_SELECT, (int)Key.Space);
        //this.kbJoy1.mapKey(InputHandler.KEY_UP, (int)Key.Up);
        //this.kbJoy1.mapKey(InputHandler.KEY_DOWN, (int)Key.Down);
        //this.kbJoy1.mapKey(InputHandler.KEY_LEFT, (int)Key.Left);
        //this.kbJoy1.mapKey(InputHandler.KEY_RIGHT, (int)Key.Right);
        //this.kbJoy2.mapKey(InputHandler.KEY_A, 0x4b);
        //this.kbJoy2.mapKey(InputHandler.KEY_B, 0x4d);
        //this.kbJoy2.mapKey(InputHandler.KEY_START, 0x45);
        //this.kbJoy2.mapKey(InputHandler.KEY_SELECT, 0x47);
        //this.kbJoy2.mapKey(InputHandler.KEY_UP, 0x4c);
        //this.kbJoy2.mapKey(InputHandler.KEY_DOWN, 70);
        //this.kbJoy2.mapKey(InputHandler.KEY_LEFT, 0x48);
        //this.kbJoy2.mapKey(InputHandler.KEY_RIGHT, 0x4a);
    }

    public static void keyDown(object event_sender, KeyEventArgs e)
    {
    }

    public override int Height
    {
        get
        {
            return (int) this.applet.Height;
        }
    }

    public override BufferView ImgPalView
    {
        get
        {
            return null;
        }
    }

    public override KbInputHandler Joy1
    {
        get
        {
            return this.kbJoy1;
        }
    }

    public override KbInputHandler Joy2
    {
        get
        {
            return this.kbJoy2;
        }
    }

    public override Point Location
    {
        get
        {
            return new Point(0.0, 0.0);
        }
    }

    public override BufferView NameTableView
    {
        get
        {
            return null;
        }
    }

    public override NES NES
    {
        get
        {
            return this.nes;
        }
    }

    public override BufferView PatternView
    {
        get
        {
            return null;
        }
    }

    public override BufferView ScreenView
    {
        get
        {
            return this.vScreen;
        }
    }

    public override BufferView SprPalView
    {
        get
        {
            return null;
        }
    }

    public override int Width
    {
        get
        {
            return (int) this.applet.Width;
        }
    }
}

